import { request } from '@umijs/max';
import { message } from 'antd';


export async function queryList(data: any) {
  const resp = await request('/api/v1/products/list', { method: 'POST', data})
  return resp.data
}

export async function save(data: any) {
  return await request('/api/v1/products/insert', { method: 'POST', data})
}

export async function importFrom1688(data: any) {
  return await request('/api/v1/products/importFrom1688', { method: 'POST', data})
}

export async function genId() {
  const resp = await request('/api/v1/common/genID', { method: 'POST'})
  return resp.data
}

export const deleteRequest = async (id: string) => {
  return await request('/api/v1/products/delete', { method: 'POST', data: { id }})
}

export const detailRequest = async (id: string) => {
  const data = await request(`/api/v1/products/detail`, { method: 'POST', data: { id } });
  if (!data.success) {
    return message.error('数据不存在');
  }
  return data.data;
}

export const listRequest = async (data: any) => {
  return await request('/api/v1/products/list', { 
    method:'POST', data
  });
}

export const saveRequest = async (value: any) => {
  let resp;
  if (value.id && value.id !== '') {
    resp = await request(`/api/v1/products/update`, { method: 'POST', data: value });
  }
  else {
    resp = await request(`/api/v1/products/insert`, { method: 'POST', data: value });
  }
  return resp;
}